@include "reader.awk"
@include "misc.awk"
@include "euclid.awk"
@include "bestKeep.awk"

function best(   _local) { 
    args("sep=,;osep=,;in=/dev/stdin",Opt,_local)
    OFS = Opt["osep"]; FS = Opt["sep"]
    bestMain(Opt["in"])
}
function bestMain(f,t,        c) {
    lines(f,t,"bestLine")
    for(c=t["cols"];c>=1;c--) 
        if (c in t["nonclass"]) {
            report(t,c,"+")
            report(t,c,"-") 
        } 
}
function bestLine(t,r,n) {
    line(t,r,n)
    if(n>1) {
        t["h"][r[t["cols"]]]++
        keep(t)
    }
}
function report(t,c,k) {
    print c in t["num"] ? reportNum(t,c,k) : reportSym(t,c,k)
}
function reportNum(t,c,k,    mean,sd,some) {
    some = 10^(-10)
    sum = t["sum"][k][c]
    n   = t["n"][k][c] + some
    sumSq=t["sumSq"][k][c]
    mean = sum/n
    sd   = sd(sumSq,sum,n)
    return t["name"][c] OFS k OFS f2s("3.1",mean) OFS f2s("3.1",sd)
}
function reportSym(t,c,k,  val,f) {
    val = t["countSym"][k][c] 
    f = round(100*t["count"][k][c][val] / t["h"][k] )
   # percent = round(100*t["count"][k][c][val] / class)
    return t["name"][c] OFS k OFS val OFS f
}
